using System.Collections.Generic;
using NodeCanvas.DialogueTrees;
using UnityEngine;

public class ChoiceUI : MonoBehaviour
{
	public Transform dialogTable;

	public ChoiceButton buttonPrefab;

	public UISprite titleBck;

	public UILabel title;

	private List<ChoiceButton> buttons = new List<ChoiceButton>();

	private float buttonHeight;

	private float titleHeight;

	private DialogueOptionsInfo info;

	private bool initComplete;

	private void Init()
	{
		if (!initComplete)
		{
			base.gameObject.SetActive(false);
			buttons.Add(buttonPrefab);
			Vector3[] worldCorners = titleBck.worldCorners;
			titleHeight = Mathf.Abs(worldCorners[0].y - worldCorners[1].y);
			Vector3[] worldCorners2 = buttonPrefab.GetComponent<UISprite>().worldCorners;
			buttonHeight = Mathf.Abs(worldCorners2[0].y - worldCorners2[1].y);
			initComplete = true;
		}
	}

	private void Awake()
	{
		Init();
	}

	public void ShowChoices(DialogueOptionsInfo info)
	{
		Init();
		base.gameObject.SetActive(true);
		this.info = info;
		foreach (ChoiceButton button in buttons)
		{
			button.gameObject.SetActive(false);
		}
		int num = 0;
		float num2 = 0f - buttonHeight;
		foreach (KeyValuePair<Statement, int> finalOption in info.finalOptions)
		{
			if (num >= buttons.Count)
			{
				ChoiceButton component = NGUITools.AddChild(dialogTable.gameObject, buttonPrefab.gameObject).GetComponent<ChoiceButton>();
				component.transform.position = buttonPrefab.transform.position + Vector3.down * ((float)buttons.Count * buttonHeight);
				buttons.Add(component);
			}
			ChoiceButton choiceButton = buttons[num];
			choiceButton.gameObject.SetActive(true);
			num2 += buttonHeight;
			choiceButton.Init(finalOption, OnButtonClicked);
			num++;
		}
		Transform transform = dialogTable.transform;
		float z = transform.position.z;
		dialogTable.transform.position = new Vector3(0f, num2 * 0.5f, z);
	}

	public void OnButtonClicked(KeyValuePair<Statement, int> pair)
	{
		info.SelectOption(pair.Value);
		base.gameObject.SetActive(false);
	}
}
